package kali.wpj.kali.web;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebView;
import android.widget.Button;

import com.activeandroid.util.Log;
import com.github.johnpersano.supertoasts.SuperToast;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.marshalchen.common.commonUtils.urlUtils.HttpUtilsAsync;

import org.apache.http.Header;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import kali.wpj.kali.R;
import kali.wpj.kali.util.EvaluationTranslationTools;
import kali.wpj.kali.util.SDCardUtils;
import kali.wpj.kali.util.StringTools;

public class OnlineSeeActivity extends ActionBarActivity {
    @InjectView(R.id.web_result)
    WebView webResult;
    @InjectView(R.id.btn_down)
    Button btn_download;
    private String  url;
    SuperToast superToast ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online_see);
        Intent intent=getIntent();
        url=intent.getStringExtra("url");
        ButterKnife.inject(this);
        superToast = new SuperToast(OnlineSeeActivity.this);
        Log.i("url-----------------------------:",url+intent);
        webResult.getSettings().setUseWideViewPort(true);
        webResult.getSettings().setBuiltInZoomControls(true);
        webResult.loadUrl(url);
    }


    @OnClick(R.id.btn_down)
    public void doenload(){
    HttpUtilsAsync.get(url, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int i, Header[] headers, byte[] bytes) {
            if(SDCardUtils.isSDCardEnable()){
                final String path=SDCardUtils.getSDCardPath();
                InputStream inputStream = null;
                inputStream=new ByteArrayInputStream(bytes);
                String tem = StringTools.convertStreamToString(inputStream);
                tem = EvaluationTranslationTools.toTranlattion(tem);
                System.out.println("--->" + tem);
                InputStream inputStream1 = new ByteArrayInputStream(tem.getBytes());
                final String reportPath=path+"TimeFSafe";
                if (SDCardUtils.writeToSDFromInput(reportPath, url.substring(url.lastIndexOf("/") + 1) + ".html", inputStream1) == null) {
                    superToast.setDuration(SuperToast.Duration.LONG);
                    superToast.setText( getString(R.string.loadFail));
                    superToast.setIcon(SuperToast.Icon.Dark.INFO, SuperToast.IconPosition.LEFT);
                    superToast.show();
                   return ;
               }
                superToast.setDuration(SuperToast.Duration.LONG);
                superToast.setText( getString(R.string.su));
                superToast.setIcon(SuperToast.Icon.Dark.SAVE, SuperToast.IconPosition.LEFT);
                superToast.show();
            }else{
                superToast.setDuration(SuperToast.Duration.LONG);
                superToast.setText( getString(R.string.sdnofound));
                superToast.setIcon(SuperToast.Icon.Dark.EXIT, SuperToast.IconPosition.LEFT);
                superToast.show();

                return;
            }
        }

        @Override
        public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            superToast.setDuration(SuperToast.Duration.LONG);
            superToast.setText(getString(R.string.failtryIt));
            superToast.setIcon(SuperToast.Icon.Dark.INFO, SuperToast.IconPosition.LEFT);
            superToast.show();

        }
    });
    }
}
